<template>
    <div class="set">
        <van-nav-bar title="个人资料" left-text="返回" @click-left="pathGo" left-arrow />
        <van-cell-group>
            <van-field label="姓名" @keydown.enter="disInput('username', $event)" @click="change($event)" v-model="username" disabled />
            <van-field label="密码" @keydown.enter="disInput('password', $event)" @click="change($event)" v-model="password" disabled />
            <van-field label="签名" @keydown.enter="disInput('txt', $event)" @click="change($event)" v-model="txt" disabled />
        </van-cell-group>
    </div>
</template>

<script>
export default {
    name: "set",
    data() {
        return {
            username: "lineo",
            password: "888888",
            txt: "天青色等烟雨",
            num1: 0,
            num2: 0,
            flat: true,
            tokenIndex: 0,
            initName: "",
        };
    },
    methods: {
        pathGo() {
            this.$router.go(-1);
        },
        change(e) {
            if (this.flat) {
                this.num1 = new Date().getTime();
                this.flat = false;
            } else {
                this.num2 = new Date().getTime();
                this.flat = true;
            }

            if (this.num1 != 0 && this.num2 != 0) {
                if (this.num1 > this.num2) {
                    if (this.num1 - this.num2 < 300) {
                        console.log(this.num1 - this.num2);
                        e.target.disabled = false;
                        this.num1 = 0;
                        this.num2 = 0;
                    }
                } else {
                    if (this.num2 - this.num1 < 300) {
                        console.log(this.num2 - this.num1);
                        e.target.disabled = false;

                        this.num1 = 0;
                        this.num2 = 0;
                    }
                }
            }
        },
        disInput(x, e) {
            let loginToken = JSON.parse(localStorage.getItem("loginUser"));
            e.target.disabled = true;
            let tokenList = JSON.parse(localStorage.getItem("token"));
            if (x == "username") {
                let tl = tokenList.findIndex((item) => {
                    return item["username"] == this.username;
                });
                if (tl == -1) {
                    if (localStorage.getItem("record")) {
                        let recordList = JSON.parse(localStorage.getItem("record"));
                        recordList.map((item) => {
                            if (item["yyid"] == this.initName) {
                                item["yyid"] = this.username;
                            }
                        });
                        localStorage.setItem("record", JSON.stringify(recordList));
                        this.initName = loginToken["username"];
                    }
                    if (localStorage.getItem("flat")) {
                        let flatList = JSON.parse(localStorage.getItem("flat"));

                        flatList.map((item) => {
                            if (item["yyid"] == this.initName) {
                                item["yyid"] = this.username;
                            }
                        });
                        localStorage.setItem("flat", JSON.stringify(flatList));
                        this.initName = loginToken["username"];
                        loginToken[x] = this[x];
                        tokenList[this.tokenIndex] = loginToken;
                        localStorage.setItem("token", JSON.stringify(tokenList));
                        localStorage.setItem("loginUser", JSON.stringify(loginToken));
                    }
                } else {
                    alert("用户名已存在,请重新输入!");
                    this.username = this.initName;
                    return;
                }
            } else {
                this.initName = loginToken["username"];
                loginToken[x] = this[x];
                tokenList[this.tokenIndex] = loginToken;
                localStorage.setItem("token", JSON.stringify(tokenList));
                localStorage.setItem("loginUser", JSON.stringify(loginToken));
            }
        },
        getLoginToken() {
            let loginToken = JSON.parse(localStorage.getItem("loginUser"));
            let tokenList = JSON.parse(localStorage.getItem("token"));
            this.tokenIndex = tokenList.findIndex((item) => {
                return item.username == loginToken.username;
            });
            this.initName = loginToken["username"];
            this.username = loginToken["username"];
            this.password = loginToken["password"];
            if (loginToken["txt"]) {
                this.txt = loginToken["txt"];
            }
        },
    },
    created() {
        this.getLoginToken();
    },
};
</script>

<style lang="scss" scope>
@import url(../../static/style/reset.css);
@import "../../static/style/variable.scss";
.set {
    .van-nav-bar__text,
    .van-nav-bar .van-icon {
        color: $activeColor;
    }
}
</style>
